package com.seek.util;

import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
  
public class SinaShortUrl {  
  
    public static String url = "http://api.t.sina.com.cn/short_url/shorten.json?";
    
	public static String getShortUrl(String longUrl) throws Exception{
    	try{
    		String content = SinaShortUrl.getURLEncodeString("source", "5786724301");
    		content +="&" + getURLEncodeString("url_long", longUrl);
    		String ret = HttpRequestUtils.readContentFromPostOld(url, content);

    		int pos1 = ret.indexOf("url_short");
    		if (pos1 > 0){
    			String retUrl = ret.substring(pos1 + 12, ret.indexOf(",", pos1));
    			retUrl = retUrl.substring(0, retUrl.length() - 1);
    			return retUrl;
    		}
            return "";
    	}catch(Exception e){
    		e.printStackTrace();
    		return "";
    	}
    }
    
	public static void main(String[] args) {  
    	try{
    		Calendar c = Calendar.getInstance();
    		c.setTime(new Date());
    		long b = c.getTimeInMillis();
    		String url = SinaShortUrl.getShortUrl("http://qmjs.xiaohongquan.cn/mall/html/home.html");
    		c.setTime(new Date());
    		long e = c.getTimeInMillis();
    		System.out.println("url="+url+",时间："  + (e - b));
    	}catch(Exception e){
        	
        	}
    	
    }  
  
    public static String getURLEncodeString(String key, String value) throws Exception{
    	
		return key + "=" + URLEncoder.encode(value, "UTF-8");
	}
} 
